package com.rongchuang.service;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.rongchuang.dto.SupplyDemandDTO;
import com.rongchuang.entity.QSupplyDemand;
import com.rongchuang.entity.SupplyDemand;
import com.rongchuang.repository.SupplyDemandRepository;
import com.rongchuang.util.ConverterUtil;
import com.rongchuang.util.DateUtils;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SupplyDemandService {

    @Autowired
    private JPAQueryFactory queryFactory;

    @Autowired
    private SupplyDemandRepository supplyDemandRepository;

    /**
     * 分页查询
     */
    public Page<SupplyDemandDTO> pageSearch(int pageNum, int pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        QSupplyDemand supplyDemand = QSupplyDemand.supplyDemand;
        List<SupplyDemand> supplyDemands = queryFactory
                .selectFrom(supplyDemand)
                .offset((long) pageNum * pageSize)
                .limit(pageSize)
                .orderBy(supplyDemand.id.desc())
                .fetch();
        long total = queryFactory
                .selectFrom(supplyDemand)
                .fetchCount();
        List<SupplyDemandDTO> supplyDemandDTOS = ConverterUtil.convertToList(supplyDemands, SupplyDemandDTO.class);
        return new PageImpl<>(supplyDemandDTOS, pageable, total);
    }

    /**
     * 保存供求信息
     */
    public void modify(SupplyDemandDTO dto) {
        SupplyDemand entity;
        if (Objects.isNull(dto.getId())) {
            entity = ConverterUtil.convertTo(dto, SupplyDemand.class);
            entity.setCreateTime(DateUtils.getCurrentTime());
            entity.setStatus((byte) 1);
        }else {
            entity = supplyDemandRepository.getById(dto.getId());
            if (StringUtils.isNotBlank(dto.getContactPerson())) {
                entity.setContactPerson(dto.getContactPerson());
            }
            if (StringUtils.isNotBlank(dto.getContactPhone())) {
                entity.setContactPhone(dto.getContactPhone());
            }
            if (StringUtils.isNotBlank(dto.getDescription())) {
                entity.setDescription(dto.getDescription());
            }
            if (StringUtils.isNotBlank(dto.getTitle())) {
                entity.setTitle(dto.getTitle());
            }
            if (Objects.nonNull(dto.getType())) {
                entity.setType(dto.getType());
            }
            if (Objects.nonNull(dto.getStatus())) {
                entity.setStatus(dto.getStatus());
            }
        }
        entity.setUpdateTime(DateUtils.getCurrentTime());
        supplyDemandRepository.save(entity);
    }

    /**
     * 获取供求信息
     * @param id 会员id
     */
    public SupplyDemandDTO getDetail(Long id) {
        SupplyDemand supplyDemand = supplyDemandRepository.getById(id);
        return ConverterUtil.convertTo(supplyDemand, SupplyDemandDTO.class);
    }
}
